﻿<?php
/*
* 银联的退款后台处理地址
* 模仿微信的退款处理 先处理完所有事情 然后再发起退款 这边只要确认退款成功就可以了
*/
//验证签名的文件
include_once './func/common.php';
include_once './func/secureUtil.php';

require("/var/www/html/new/d/class/OrderRecord.php");//查询订单
include "/var/www/html/new/conf/le.je";
$le = new go_sql();
$le->connect();

define('LOG_NAME', '/var/www/html/new/d/logs/union_pay/logs_refund/notify_'.date('ymd').'.log');//log文件路径
define('PAY_MODE_UNI',7);

define('ACTION_MONEY_ADD',0);
define('ACTION_MONEY_CUT',1);
$sourceT = 2;//银联的
//todo 上线后打开
write_logs(json_encode($_POST), LOG_NAME);
if (isset($_POST['signature'])) {
    if(!verify ($_POST)){
        write_logs('验签失败', LOG_NAME);
        exit('chkFail');
    }
//    write_logs(json_encode($_POST), LOG_NAME);exit;//todo  测试阶段 到这里就不执行了 看下回传数据
    write_logs('验签成功', LOG_NAME);
    if($_POST['respMsg']!='Success!'){
        write_logs('返回消息不成功', LOG_NAME);
        exit('Not Success');
    }

    $trade_no = $_POST['origQryId'];
    $ordernq = str_replace('REF','',$_POST['orderId']);//银联返回的订单号 去掉开头的补足字符
    $ordern = substr($ordernq,0,strpos($ordernqq,'R'));//再去掉R和之后的数字 表示的是
    $log_id = substr($ordernq,strpos($ordernqq,'R')+1);//pft_order_refund 表里的ID
    $str  = "SELECT status,total_fee,ordernum FROM pft_alipay_rec
             WHERE trade_no='$trade_no' AND `sourceT`=$sourceT LIMIT 1";
    $GLOBALS['le']->query($sql);
    $pay_log_data = $GLOBALS['le']->fetch_assoc();
    $ordernum = $pay_log_data['ordernum'];//数据库里的订单号
    if($ordern != $ordernum){
        write_logs('返回订单号有误', LOG_NAME);
        $GLOBALS['le']->close();
        exit('Error OrderNum');
    }
    //TODO::根据交易号获取订单数据
    $handler_time = date('Y-m-d H:i:s',strtotime($_POST['txnTime']));
    $sql = "SELECT id,refund_status,refund_money FROM pft_order_refund
            WHERE ordernum='$ordernum' and trade_no='$trade_no' and handler_time=$handler_time
            and sourceT=$sourceT  and id=$log_id LIMIT 1";
//        write_logs("sql2:" . $sql);
    $GLOBALS['le']->query($sql);
    $refund_data = $GLOBALS['le']->fetch_assoc();
    if ($refund_data){
        if ($refund_data['refund_status']!=1) {
            write_logs("退款记录状态有误.订单号[{$ordernum}],银联流水号[{$trade_no}]",LOG_NAME);
            $GLOBALS['le']->close();
            //如果这个退款已经处理，不往下执行
            exit;
        }
        $tnum = $refund_data['refund_num'];
        $pay_total_fee = $_POST['txnAmt'];//银联返回的金额
        $total_fee = $refund_data['refund_money'];//数据库里的金额
        if ($pay_total_fee != $total_fee) {
            write_logs("[$ordern]支付金额不等。返回金额:{$pay_total_fee},实际金额:{$total_fee}", LOG_NAME);
            $GLOBALS['le']->close();
            exit('fail');
        }
        if ($pay_log_data['status']!=2) {
            write_logs("支付记录状态有误.订单号[{$ordernum}],银联流水号[{$trade_no}]",LOG_NAME);
            $GLOBALS['le']->close();
            //如果这个退款状态不是退款中
            exit;
        }
        $sql = "UPDATE pft_alipay_rec SET status=3 WHERE out_trade_no='{$refund_data['ordernum']}' LIMIT 1";
        if(($ret=$db->exec($sql)) === false){
            write_logs('Error:' . $sql,LOG_NAME);
        }
        //batch_no填的是 退货时新产生的流水号
        $sql = "UPDATE pft_order_refund SET batch_no='{$_POST['queryId']}' WHERE id=$log_id LIMIT 1";
        if(($ret=$db->exec($sql)) === false){
            write_logs('Error:' . $sql,LOG_NAME);
        }
    }else{
        write_logs("记录不存在.订单号[{$ordernum}]银联流水号[{$trade_no}]", LOG_NAME);
        $GLOBALS['le']->close();
        exit;
    }
    write_logs('退款处理完毕', LOG_NAME);
    $GLOBALS['le']->close();
    exit;
}else{
    write_logs('签名为空', LOG_NAME);
    exit('chkLost');
}

function write_logs($txt,$file='/var/www/html/new/d/module/zax/unipay/logs/new_log.log'){
    $fp = fopen($file,"a");
    flock($fp, LOCK_EX);
    fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
    flock($fp, LOCK_UN);
    fclose($fp);
}
?>
